#ifndef _RING_BUF_H_
#define _RING_BUF_H_

#include <stdint.h>
#include <stdbool.h>
#include <pthread.h>

#ifndef RET_OK
#define RET_OK        0     /* ret ok */
#endif
#ifndef RET_ERR
#define RET_ERR     -1     /* ret err */
#endif

#define RING_BUF_DATA_LINK_LAYER        /**< link layer enable macro */
#define RING_BUF_TP_LAYER               /**< transport protocol layer enable macro */
#define RING_BUF_CHECKSUM16             /**< enable checksum16 check */
#define CLOG_RING_BUF_MAX_NUM   (1024)
#define CLOG_RING_BUF_MAX_SIZE  (1024 * 1024 * 10) /* 10M */
#define RING_BUF_DEBUG          2

typedef struct {
    uint8_t rc;
    uint8_t *tmp_buf;             /**< tmp buffer */
    uint8_t *buf;                 /**< buffer */
    size_t size;                  /**< size */
    int32_t frag_cnt_set;         /**< fragment count set */
    int32_t frag_cnt_total;       /**< fragment count total */
    int32_t frag_last_copy;       /**< fragment last copy */
    int32_t frag_index;           /**< fragment index */
    int32_t frag_finish_flag;     /**< fragment finish flag */
} ring_buf_swap_t;

typedef struct {
    size_t head;                /**< ring buf head */
    size_t tail;                /**< ring buf tail */
    size_t size;                /**< ring buf size */
    size_t capacity;            /**< ring buf capacity */
} ring_buf_status_t;

typedef struct {
    int32_t init_status;        /**< init status */
    int32_t debug_level;        /**< debug level */
    uint8_t *ring_buf;          /**< ring buffer */
    size_t head;                /**< ring buf head */
    size_t tail;                /**< ring buf tail */
    size_t size;                /**< ring buf size */
    size_t capacity;            /**< ring buf capacity */
    pthread_mutex_t lock;       /**< read/write lock */
    pthread_cond_t cond;        /**< read/write cond */
    ring_buf_swap_t *write_swap; /**< write swap */
    ring_buf_swap_t *read_swap;  /**< read swap */
} ring_buf_t;

int32_t ring_buf_init_interface(const size_t buf_size);
int32_t ring_buf_deinit_interface(void);
int32_t put_data_to_ring_buf_interface(uint8_t *data, size_t size);
int32_t get_data_from_ring_buf_interface(uint8_t *data, size_t *size);
int32_t set_ring_buf_debug_level_interface(int32_t level);
int32_t get_ring_buf_debug_level_interface(void);
int32_t get_ring_buf_status_interface(ring_buf_status_t *ring_buf_status);
int32_t get_ring_buf_used_size_interface(void);
#endif /* _RING_BUF_H_ */
